import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ij.ImagePlus;
import ij.ImageStack;
import ij.plugin.PlugIn;

public class Micromanager_Montage implements PlugIn{
	//static String slash="/";//mac
	//windows
	static String slash ="\\";
	static final String URL = "C:"+slash+"Program Files"+slash+"Fiji.app";
	static final String SLASH ="/";
	static final String PROGRAM_DEFAULT_DATA = URL+slash+"StackMontage.ini";
	static final double DEFAULT_PIXEL_SIZE_GEN1 = 0.65;
	static final double DEFAULT_PIXEL_SIZE_GEN2 = 0.65;
//	static final double DEFAULT_PIXEL_SIZE_GEN3 = 0.4986;
	static final double DEFAULT_PIXEL_DEPTH     = 5.0;
	
	JButton dataButton, preButton,saveButton, toStackButton;
	JLabel dataLabel;
	File folder;
	ImageStack vS;
	ImagePlus imp;
	JComboBox<String> modeCB;
	InstructionPanel instrPanel;
	JPanel montageSettingsP;
	MontageSettings mS;
	
	public void run(String arg){
		final JFrame gui = new JFrame("Micromanager Montage");
		gui.setLayout(new GridLayout(1,2));
		JPanel leftPanel = new JPanel();
		gui.add(leftPanel);
		JPanel rightPanel = new JPanel();
		gui.add(rightPanel);

		
		instrPanel = new InstructionPanel(rightPanel);
		String s0 =  "Montage Images";
		String s1 = "Step 1: Select parent directory of data.";
		String s2 = "Step 2: Select 2P.";
		String s3 = "        Each 2P montages differently.";
		instrPanel.setHeader(s0);
		instrPanel.setInstructions(s1+"\n"+s2+"\n"+s3);
		
		leftPanel.setLayout(new BorderLayout());
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		dataButton = new JButton("Select File:");
		topPanel.add(dataButton,c);
		c.gridx=1;
		dataLabel = new JLabel("None Selected");
		topPanel.add(dataLabel,c);
		
		c.gridx=0;
		c.gridy=1;
		topPanel.add(new JLabel("2P:"),c);
		
		c.gridx=1;
		String modeChoice[] = new String[5];
		modeChoice[0] = "Gen1";
		modeChoice[1] = "Gen2 Grids";
		modeChoice[2] = "Gen2 Pos";
		modeChoice[3] = "Gen3 Grids";
		modeChoice[4] = "Gen3 Pos";
		modeCB = new JComboBox<String>(modeChoice);
		topPanel.add(modeCB,c);
		leftPanel.add(topPanel,BorderLayout.NORTH);  
		
		montageSettingsP = new JPanel();
		leftPanel.add(montageSettingsP, BorderLayout.CENTER);
		preButton = new JButton("Preview");
		saveButton = new JButton("Save");
		toStackButton = new JButton("To Stack");
		JPanel finPanel = new JPanel();
		finPanel.add(preButton);
		finPanel.add(saveButton);
		finPanel.add(toStackButton);
		leftPanel.add(finPanel, BorderLayout.SOUTH);
		

		mS = new MontageSettings(montageSettingsP, instrPanel);
		
		gui.pack();
		gui.setSize(600,450);
		gui.setVisible(true);
		gui.validate();
		
		dataButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String CurrentLocation = URL;
				try{
					FileInputStream Location = new FileInputStream(PROGRAM_DEFAULT_DATA);
					BufferedReader TempReader = new BufferedReader(new InputStreamReader(Location));
					CurrentLocation = TempReader.readLine();
					TempReader.close();
					
				}
				catch(Exception InitialLocationNotFound){
					try{	
						FileOutputStream Location = new FileOutputStream(PROGRAM_DEFAULT_DATA);
						BufferedWriter TempWriter = new BufferedWriter(new OutputStreamWriter(Location));
						TempWriter.write(""+CurrentLocation);
						TempWriter.close();
					}catch(Exception EE){}
				}
				JFileChooser chooser = new JFileChooser(CurrentLocation);
				chooser.setDialogTitle("Choose Parent folder of grids");
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.showOpenDialog(null);
				folder = chooser.getSelectedFile();
				try{
					FileOutputStream Location = new FileOutputStream(PROGRAM_DEFAULT_DATA);
					BufferedWriter TempWriter = new BufferedWriter(new OutputStreamWriter(Location));
					CurrentLocation = folder.getAbsolutePath();
					TempWriter.write(""+CurrentLocation);
					TempWriter.close();
				}catch(Exception EEE){}	
				dataLabel.setText(folder.getName());
				callMontageSettings();
			}
		});
		modeCB.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				callMontageSettings();
				gui.validate();
				
			}
		});
		preButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				mS.create();
			}
		});
		saveButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				JFileChooser chooser2 = new JFileChooser(folder.getAbsoluteFile());
				chooser2.setDialogTitle("Choose Parent folder of grids");
				chooser2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser2.showOpenDialog(null);
				File savefolder = chooser2.getSelectedFile();
				mS.save(savefolder);
			}
		});
		toStackButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				mS.toStack();
			}
		});
	}
	public void callMontageSettings(){
		mS.fillMenu( folder, modeCB.getSelectedIndex());
	}
}
